var request = undefined;
var equipmentData = [];
$(document).ready(function() {
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	getRealTimeStatus();
})

function getRealTimeStatus() {
	var ajax = new createAjax();
	ajax.type = "post";
	ajax.dataType = "json";
	ajax.contentType = "application/json";
	ajax.url = "realTimeStatus";
	ajax.timeout = 3000;
	ajax.success = function(data, textStatus) {
		equipmentData = data;
		var length = data.length;
		var chargingpile = $('#chargingpile');
		var position = $('#position');
		var position2 = $('#position2');
		$('#chargingpile').html('');
		$('#position').html('');
		$('#position2').html('');
		for(var i=0; i<length; i++){
			//充电机
			var chargingPile = document.createElement("span");
			chargingPile.setAttribute("id", "chargingPile" + (i+1));
			chargingPile.setAttribute("onclick", "getDetatil("+i+")");
			//图标
			var Position = document.createElement("span");
			var Position2 = document.createElement("span");
			Position2.setAttribute("id","position00" + (i+1));
			Position2.setAttribute("class","choose");
			if(data[i].communicationState == "正常"){
//				document.getElementById(data[i].DevCode.equipmentNo).src = "static/images/electricize01.png";
				Position.setAttribute("id", "position" + (i+1));
				Position.setAttribute("class","position02");
				if(data[i].deviceState == "充电中"){
					chargingPile.setAttribute("class", "chargingPile02");
				} else if(data[i].deviceState == "空闲"){
					chargingPile.setAttribute("class", "chargingPile05");
				} else {
					chargingPile.setAttribute("class", "chargingPile04");
				}
			} else if(data[i].communicationState == "断开"){
//				document.getElementById(data[i].DevCode.equipmentNo).src = "static/images/breakdown01.png";
				Position.setAttribute("id", "position" + (i+1));
				Position.setAttribute("class","position03");
				chargingPile.setAttribute("class", "chargingPile03");
			}
			
			//添加充电桩
			chargingpile.append(chargingPile);
			//显示图标
			position.append(Position);
			position.append(Position2);
		}
		getRealTimeStatus();
	};
	ajax.error = function(XMLHttpRequest, textStatus, errorThrown) {
		if (textStatus == "timeout") {
			getRealTimeStatus();
		} else if (textStatus != "abort") {
			getRealTimeStatus();
		}
	};
	if (request) {
		request.abort();
	}
	request = startAjax(ajax);
	ajax = null;
}
function getDetatil(i){
	window.parent.document.getElementById("selectEquipment").value = equipmentData[i].DevCode.equipmentName;
	window.location.href = "chargingEquipment/equipmentStatus";
}